package intjavatorpedo.client;

import intjavatorpedo.ws.TorpedoImplService;
import intjavatorpedo.ws.TorpedoInterface;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class LoginWindow extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTextField;
	private JPanel newUserPanel;
	private JPanel playPanel;
	private UserListModel userListModel;
	
	private TorpedoInterface torpedo;
	private JLabel lblSelectAUser;
	private JList<String> opponentList;
	private JList<String> userList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		// Init webservice proxy
		TorpedoImplService service = new TorpedoImplService();
		final TorpedoInterface torpedo = service.getTorpedoImplPort();
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginWindow frame = new LoginWindow(torpedo);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginWindow(TorpedoInterface torpedo) {
		this.torpedo = torpedo;
		initComponents();
	}

	private void initComponents() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 850, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		newUserPanel = new JPanel();
		sl_contentPane.putConstraint(SpringLayout.NORTH, newUserPanel, 10, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, newUserPanel, 10, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, newUserPanel, 43, SpringLayout.NORTH, contentPane);
		contentPane.add(newUserPanel);
		
		JLabel lblPleaseProvideYour = new JLabel("Please provide your user name: ");
		newUserPanel.add(lblPleaseProvideYour);
		
		userNameTextField = new JTextField();
		newUserPanel.add(userNameTextField);
		userNameTextField.setColumns(10);
		
		JButton newUserButton = new JButton("Add new user");
		newUserButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String userName = userNameTextField.getText();
				if (!userName.isEmpty()) {
					boolean success = torpedo.addUser(userName);
					if (!success)
						JOptionPane.showMessageDialog(getContentPane(), "There is a user with the same name.");
					else
						userListModel.refresh();
				}
			}
		});
		newUserPanel.add(newUserButton);
		
		playPanel = new JPanel();
		sl_contentPane.putConstraint(SpringLayout.NORTH, playPanel, 5, SpringLayout.SOUTH, newUserPanel);
		sl_contentPane.putConstraint(SpringLayout.WEST, playPanel, 5, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, playPanel, -5, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, playPanel, -5, SpringLayout.EAST, contentPane);
		contentPane.add(playPanel);
		SpringLayout sl_playPanel = new SpringLayout();
		playPanel.setLayout(sl_playPanel);
		
		this.userListModel = new UserListModel(this.torpedo);
		this.userListModel.refresh();
		
		lblSelectAUser = new JLabel("Select a user to play with:");
		sl_playPanel.putConstraint(SpringLayout.NORTH, lblSelectAUser, 10, SpringLayout.NORTH, playPanel);
		sl_playPanel.putConstraint(SpringLayout.WEST, lblSelectAUser, 10, SpringLayout.WEST, playPanel);
		playPanel.add(lblSelectAUser);
		
		JScrollPane leftScrollPane = new JScrollPane();
		//sl_playPanel.putConstraint(SpringLayout.EAST, leftScrollPane, -350, SpringLayout.EAST, playPanel);
		sl_playPanel.putConstraint(SpringLayout.WEST, leftScrollPane, 0, SpringLayout.WEST, lblSelectAUser);
		sl_playPanel.putConstraint(SpringLayout.NORTH, leftScrollPane, 5, SpringLayout.SOUTH, lblSelectAUser);
		sl_playPanel.putConstraint(SpringLayout.SOUTH, leftScrollPane, -10, SpringLayout.SOUTH, playPanel);
		playPanel.add(leftScrollPane);
		
		userList = new JList<String>();
		//sl_playPanel.putConstraint(SpringLayout.NORTH, userList, 208, SpringLayout.NORTH, playPanel);
		//sl_playPanel.putConstraint(SpringLayout.SOUTH, userList, -223, SpringLayout.SOUTH, playPanel);
		//sl_playPanel.putConstraint(SpringLayout.EAST, userList, 266, SpringLayout.WEST, playPanel);
		//sl_playPanel.putConstraint(SpringLayout.WEST, userList, 225, SpringLayout.WEST, playPanel);
		userList.setModel(userListModel);
		userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		leftScrollPane.setViewportView(userList);
		
		JButton btnPlay = new JButton("Play");
		sl_playPanel.putConstraint(SpringLayout.NORTH, btnPlay, 10, SpringLayout.NORTH, lblSelectAUser);
		sl_playPanel.putConstraint(SpringLayout.WEST, btnPlay, 50, SpringLayout.EAST, leftScrollPane);
		//sl_playPanel.putConstraint(SpringLayout.EAST, btnPlay, 108, SpringLayout.EAST, leftScrollPane);
		btnPlay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String selectedUser = userList.getSelectedValue();
				String opponentUser = opponentList.getSelectedValue();
				int gameNumber = torpedo.startGameWithUser(selectedUser, opponentUser, true);
				if (gameNumber == -1)
					JOptionPane.showMessageDialog(getContentPane(), "The user and the opponent can't be the same!");
				else {
					MainWindow mainWindow = new MainWindow(torpedo, gameNumber, selectedUser, opponentUser);
					mainWindow.setVisible(true);
					SwingUtilities.getWindowAncestor(getContentPane()).setVisible(false);
				}
			}
		});
		playPanel.add(btnPlay);

		JLabel lblSelectLabel = new JLabel("Select an opponent:");
		sl_playPanel.putConstraint(SpringLayout.WEST, lblSelectLabel, 50, SpringLayout.EAST, btnPlay);
		sl_playPanel.putConstraint(SpringLayout.NORTH, lblSelectLabel, 0, SpringLayout.NORTH, lblSelectAUser);
		playPanel.add(lblSelectLabel);
		
		JScrollPane rightScrollPane = new JScrollPane();
		sl_playPanel.putConstraint(SpringLayout.NORTH, rightScrollPane, 5, SpringLayout.SOUTH, lblSelectLabel);
		sl_playPanel.putConstraint(SpringLayout.WEST, rightScrollPane, 0, SpringLayout.WEST, lblSelectLabel);
		sl_playPanel.putConstraint(SpringLayout.SOUTH, rightScrollPane, -10, SpringLayout.SOUTH, playPanel);
		//sl_playPanel.putConstraint(SpringLayout.EAST, rightScrollPane, -10, SpringLayout.EAST, playPanel);
		playPanel.add(rightScrollPane);
		
		opponentList = new JList<String>();
		//sl_playPanel.putConstraint(SpringLayout.NORTH, opponentList, 50, SpringLayout.SOUTH, lblSelectLabel);
		//sl_playPanel.putConstraint(SpringLayout.EAST, opponentList, -5, SpringLayout.EAST, playPanel);
		opponentList.setModel(this.userListModel);
		opponentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		rightScrollPane.setViewportView(opponentList);
		
		this.setResizable(false);
	}
}
